package com.pig.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pig.pojo.ImageVo;
import com.pig.pojo.recipes.Recipes;
import com.pig.service.FileService;
import com.pig.service.recipes.RecipesService;
import com.pig.util.PageObject;
import com.pig.util.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/recipes")
public class RecipesControllerManage {
    @Reference
    private RecipesService recipesService;

    @Autowired
    @Qualifier("fileUploadServiceImpl")
    private FileService fileService;

    @RequestMapping("/doFindRecipes")
    public SysResult doFindRecipes(String username, Integer pageCurrent){
        PageObject<Recipes> recipesPageObject = recipesService.findObject(username,pageCurrent);
        return SysResult.success(recipesPageObject);
    }

    @RequestMapping("/doFindrecipesPage")
    public SysResult doFindrecipesPage(String title, Integer pageCurrent){
        PageObject recipesPages = recipesService.findObjectPage(title,pageCurrent);
        return SysResult.success(recipesPages);
    }
    @RequestMapping("/doFindRecipesById")
    public SysResult doFindObjectsById(Integer id){
        return SysResult.success(recipesService.findObjectById(id));
    }

    @RequestMapping("/doAddRecipes")
    public SysResult doAddRecipes(@RequestBody Recipes recipes){
        recipesService.AddRecipes(recipes);
        return SysResult.success("添加成功");
    }
    @RequestMapping("/doUpdateRecipes")
    public SysResult doUpdateRecipes(@RequestBody Recipes recipes){
        recipesService.UpdateRecipes(recipes);
        System.out.println(recipes);
        return SysResult.success("更新成功");
    }
    @RequestMapping("/doDeleteById")
    public SysResult deleteById(@RequestParam Long recipesId){

        recipesService.deleteById(recipesId);

        return SysResult.success();
    }
    @RequestMapping("/file")
    public ImageVo file(@RequestParam("file") MultipartFile file){
        return fileService.upload(file,0 );
    }

}
